home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / libg_261.zip / libg_261 / libg++ / src / gen / CHMap.ccP < prev    next >
Text File  |  1992-04-14  |  4KB  |  167 lines

  1. // This may look like C code, but it is really -*- C++ -*-
  2. /* 
  3. Copyright (C) 1988 Free Software Foundation
  4.     written by Doug Lea (dl@rocky.oswego.edu)
  5.  
  6. This file is part of the GNU C++ Library.  This library is free
  7. software; you can redistribute it and/or modify it under the terms of
  8. the GNU Library General Public License as published by the Free
  9. Software Foundation; either version 2 of the License, or (at your
  10. option) any later version.  This library is distributed in the hope
  11. that it will be useful, but WITHOUT ANY WARRANTY; without even the
  12. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  13. PURPOSE.  See the GNU Library General Public License for more details.
  14. You should have received a copy of the GNU Library General Public
  15. License along with this library; if not, write to the Free Software
  16. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18.  
  19. #ifdef __GNUG__
  20. #pragma implementation
  21. #endif
  22. #include "<T>.<C>.CHMap.h"
  23.  
  24. // The nodes are linked together serially via a version
  25. // of a trick used in some vtables: odd pointers are
  26. // actually links to the next table entry. 
  27. // Not terrible, but not wonderful either
  28.  
  29. static inline int goodCHptr(<T><C>CHNode* t)
  30. {
  31.   return ((((unsigned)t) & 1) == 0);
  32. }
  33.  
  34. static inline <T><C>CHNode* index_to_CHptr(int i)
  35. {
  36.   return (<T><C>CHNode*)((i << 1) + 1);
  37. }
  38.  
  39. static inline int CHptr_to_index(<T><C>CHNode* t)
  40. {
  41.   return ( ((unsigned) t) >> 1);
  42. }
  43.  
  44. <T><C>CHMap::<T><C>CHMap(<C&> dflt, unsigned int sz)
  45.      :<T><C>Map(dflt)
  46. {
  47.   tab = (<T><C>CHNode**)(new <T><C>CHNodePtr[size = sz]);
  48.   for (unsigned int i = 0; i < size; ++i) tab[i] = index_to_CHptr(i+1);
  49.   count = 0;
  50. }
  51.  
  52. <T><C>CHMap::<T><C>CHMap(<T><C>CHMap& a) :<T><C>Map(a.def)
  53. {
  54.   tab = (<T><C>CHNode**)(new <T><C>CHNodePtr[size = a.size]);
  55.   for (unsigned int i = 0; i < size; ++i) tab[i] = index_to_CHptr(i+1);
  56.   count = 0;
  57.   for (Pix p = a.first(); p; a.next(p)) (*this)[a.key(p)] = a.contents(p);
  58. }
  59.  
  60.  
  61. Pix <T><C>CHMap::seek(<T&> key)
  62. {
  63.   unsigned int h = <T>HASH(key) % size;
  64.  
  65.   for (<T><C>CHNode* t = tab[h]; goodCHptr(t); t = t->tl)
  66.     if (<T>EQ(key, t->hd))
  67.       return Pix(t);
  68.  
  69.   return 0;
  70. }
  71.  
  72.  
  73. <C>& <T><C>CHMap::operator [](<T&> item)
  74. {
  75.   unsigned int h = <T>HASH(item) % size;
  76.  
  77.   for (<T><C>CHNode* t = tab[h]; goodCHptr(t); t = t->tl)
  78.     if (<T>EQ(item, t->hd))
  79.       return t->cont;
  80.  
  81.   t = new <T><C>CHNode(item, def, tab[h]);
  82.   tab[h] = t;
  83.   ++count;
  84.   return t->cont;
  85. }
  86.  
  87.  
  88. void <T><C>CHMap::del(<T&> key)
  89. {
  90.   unsigned int h = <T>HASH(key) % size;
  91.  
  92.   <T><C>CHNode* t = tab[h]; 
  93.   <T><C>CHNode* trail = t;
  94.   while (goodCHptr(t))
  95.   {
  96.     if (<T>EQ(key, t->hd))
  97.     {
  98.       if (trail == t)
  99.         tab[h] = t->tl;
  100.       else
  101.         trail->tl = t->tl;
  102.       delete t;
  103.       --count;
  104.       return;
  105.     }
  106.     trail = t;
  107.     t = t->tl;
  108.   }
  109. }
  110.  
  111.  
  112. void <T><C>CHMap::clear()
  113. {
  114.   for (unsigned int i = 0; i < size; ++i)
  115.   {
  116.     <T><C>CHNode* p = tab[i];
  117.     tab[i] = index_to_CHptr(i+1);
  118.     while (goodCHptr(p))
  119.     {
  120.       <T><C>CHNode* nxt = p->tl;
  121.       delete(p);
  122.       p = nxt;
  123.     }
  124.   }
  125.   count = 0;
  126. }
  127.  
  128. Pix <T><C>CHMap::first()
  129. {
  130.   for (unsigned int i = 0; i < size; ++i) if (goodCHptr(tab[i])) return Pix(tab[i]);
  131.   return 0;
  132. }
  133.  
  134. void <T><C>CHMap::next(Pix& p)
  135. {
  136.   <T><C>CHNode* t = ((<T><C>CHNode*)p)->tl;
  137.   if (goodCHptr(t))
  138.     p = Pix(t);
  139.   else
  140.   {
  141.     for (unsigned int i = CHptr_to_index(t); i < size; ++i) 
  142.     {
  143.       if (goodCHptr(tab[i]))
  144.       {
  145.         p =  Pix(tab[i]);
  146.         return;
  147.       }
  148.     }
  149.     p = 0;
  150.   }
  151. }
  152.  
  153.  
  154. int <T><C>CHMap::OK()
  155. {
  156.   int v = tab != 0;
  157.   int n = 0;
  158.   for (unsigned int i = 0; i < size; ++i)
  159.   {
  160.     for (<T><C>CHNode* p = tab[i]; goodCHptr(p); p = p->tl) ++n;
  161.     v &= CHptr_to_index(p) == i + 1;
  162.   }
  163.   v &= count == n;
  164.   if (!v) error("invariant failure");
  165.   return v;
  166. }
  167.